package com.sxbbc.plat.controller.banks;


import com.function.common.GetRest;

import com.sxbbc.bank.service.ISysProductTypeService;
import com.sxbbc.common.basics.rest.RestResponse;
import com.sxbbc.common.core.entity.banks.SysProductType;
import com.sxbbc.common.core.utils.BasePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * <p>
 * 产品类型表 前端控制器
 * </p>
 *
 * @author
 * @since 2020-09-04
 */
@Controller
public class SysProductTypeController {

    @Autowired
    private ISysProductTypeService sysProductTypeService;


    @RequestMapping(value = "/productType/list")
    public ModelAndView list() {
        ModelAndView view = new ModelAndView("/banks/productType/index");
        return view;
    }

    @RequestMapping(value = "/productType/productTypeSave")
    public ModelAndView showProductType(SysProductType sysProductType) {
        ModelAndView view = new ModelAndView("/banks/productType/productTypeSave");
        view.addObject("sysProductType",sysProductType);
        return view;
    }

    @RequestMapping(value = "/productType/deactivate")
    @ResponseBody
    public com.function.common.RestResponse deactivateProductType(Integer id){
        SysProductType sysProductType = sysProductTypeService.getById(id);
        if (sysProductType != null){
            if (sysProductType.getStatus() == 0){
                return GetRest.getFail("该类型已停用");
            }
            sysProductType.setStatus(0);
            boolean saved = sysProductTypeService.updateById(sysProductType);
            if (!saved) {
                return GetRest.getFail("停用失败");
            }
            return GetRest.getSuccess("停用成功");
        }else {
            return GetRest.getFail("该产品类型不存在，请重新再试");
        }
    }

    @RequestMapping(value = "/productType/enable")
    @ResponseBody
    public com.function.common.RestResponse enableProductType(Integer id){
        SysProductType sysProductType = sysProductTypeService.getById(id);
        if (sysProductType != null){
            if (sysProductType.getStatus() == 1){
                return GetRest.getFail("该类型已启用");
            }
            sysProductType.setStatus(1);
            boolean saved = sysProductTypeService.updateById(sysProductType);
            if (!saved) {
                return GetRest.getFail("启用失败");
            }
            return GetRest.getSuccess("启用成功");
        }else {
            return GetRest.getFail("该产品类型不存在，请重新再试");
        }
    }

    @RequestMapping(value ="/productType/listDate")
    @ResponseBody
    public RestResponse selectProductTypeListDate(BasePage<SysProductType> page, SysProductType sysProductType, String yyTime) {
        return sysProductTypeService.selSysProductType(page,sysProductType,yyTime);
    }


    @RequestMapping(value = "productType/save")
    @ResponseBody
    public com.function.common.RestResponse handleSaveProductTypeRepository(SysProductType sysProductType) {
        if ("".equals(sysProductType.getProdName()) && sysProductType.getProdName() == null){
            return GetRest.getFail("请输入类型名");
        }
        if ("".equals(sysProductType.getProdCode()) && sysProductType.getProdCode() == null){
            return GetRest.getFail("请输入类型编码");
        }
        if (sysProductType.getStatus() == null){
            return GetRest.getFail("请输入状态");
        }
        if ("".equals(sysProductType.getTypeGrade()) && sysProductType.getTypeGrade() == null){
            return GetRest.getFail("请输入类型等级");
        }
        sysProductType.setCreateTime(new Date());
        if (sysProductType.getId() == null){
            return sysProductTypeService.addSysProductType(sysProductType);
        }else {
            return sysProductTypeService.updateSysProductType(sysProductType);
        }
    }

}
